<?php

namespace app\index\controller;

use app\model\WithdrawMode;
use think\exception\ValidateException;
use app\model\Withdraw;
use app\model\Member;
use app\model\Agent;
use app\model\Technical;
use app\model\Store;
use app\model\Operatingcity;
use app\model\Config;

class WithdrawController extends Base
{
    public function index()
    {
        $mo = input('post.mo');
        $uuid = $this->getuuid();
        $where['uniacid'] = uniacid();
        if ($mo == 'agent') {
            $uid = UID();
        } elseif ($mo == 'technical') {
            $uid = $uuid;
        } elseif ($mo == 'store') {
            $uid = $uuid;
        } elseif ($mo == 'operatingcity') {
            $uid = $uuid;
        } elseif ($mo == 'balance') {
            $uid = UID();
        }

        $where['uid'] =$uid;
        $where['mo']=$mo;
        $data = Withdraw::where($where)
            ->order('id desc')
            ->select()
            ->toArray();

        foreach ($data as &$vo) {
            if (!empty($vo['create_time'])) {
                $vo['create_time'] = time_format($vo['create_time']);
            }
            if (!empty($vo['pay_time'])) {
                $vo['pay_time'] = time_format($vo['pay_time']);
            }
        }

        return $this->json(['data' => $data]);
    }

    public function calculate()
    {
        $mo = input('post.mo');
        $amounts = input('post.amounts');
        $Config = Config::getconfig($mo);
        $actual_amounts = $amounts;
        $poundage = 0;
        if (floatval($Config['cash_service_charge']) > 0) {
            $poundage = round($amounts * floatval($Config['cash_service_charge']) / 100, 2);
            $actual_amounts = round($amounts - $poundage, 2);
        }
        return $this->json(['data' => [
            'actual_amounts' => $actual_amounts,
            'poundage' => $poundage,
        ]]);

    }

    public function apply()
    {
        $message = '返回消息';
        $data = [];
        $uid = UID();
        $uuid = $this->getuuid();

        $amounts = input('post.amounts');
        $bid = input('post.bid');
        $mo = input('post.mo');
        $payWay = input('post.pay_way');
        if(empty($payWay)){
            return $this->json([
                'message' => '请选择收款方式',
                'errno' => 1,
                'data' => []
            ]);
        }

        $Config = Config::getconfig($mo);
        if (floatval($amounts) < $Config['min_money']) {
            return $this->json([
                'message' => '不能小于' . $Config['min_money'] . '元',
                'errno' => 1,
                'data' => []
            ]);
        }

        $toDayMoney = Withdraw::where([
            'uid' => $uid,
            'mo' => $mo,
            'uniacid' => uniacid()
        ])->whereTime('create_time', "today")->sum('amounts');
        if (floatval($toDayMoney + $amounts) > $Config['cash_max_day'] && $Config['cash_max_day'] > 0) {
            return $this->json([
                'message' => '一天最多提现' . $Config['cash_max_day'] . '元',
                'errno' => 1,
                'data' => []
            ]);
        }
        if ($mo == 'agent') {
            $moapply = Agent::where('uid', $uid)->find();
            $model = new Agent;
        } elseif ($mo == 'technical') {
            $moapply = Technical::where('uuid', $uuid)->find();
            $model = new Technical;
            $uid = $uuid;
        } elseif ($mo == 'store') {
            $moapply = Store::where('uuid', $uuid)->find();
            $model = new Store;
            $uid = $uuid;
        } elseif ($mo == 'operatingcity') {
            $moapply = Operatingcity::where('uuid', $uuid)->find();
            $model = new Operatingcity;
            $uid = $uuid;
        } elseif ($mo == 'balance') {
            $moapply = Member::field('balance as income')->find($uid);
            $model = new Member();
        }

        if ($moapply->income >= $amounts) {
            $param['withdraw_sn'] = $this->build_no();
            $param['mo'] = $mo;
            $param['amounts'] = $amounts;
            $actual_amounts = $amounts;
            $poundage = 0;
            if (floatval($Config['cash_service_charge']) > 0) {
                $poundage = round($amounts * floatval($Config['cash_service_charge']) / 100, 2);
                $actual_amounts = round($amounts - $poundage, 2);
            }
            $param['actual_amounts'] = $actual_amounts;
            $param['actual_poundage'] = $poundage;
            $param['status'] = 0;
            $param['uid'] = $uid;
            $param['bid'] = $bid;
            $param['create_time'] = time();
            $param["uniacid"] = uniacid();
            $param["pay_way"] = $payWay;

            $Withdraw = Withdraw::create($param);

            if ($Withdraw) {
                if ($mo == 'agent') {
                    $model->where('uid', $uid)->dec('income', $amounts)->update();
                } elseif ($mo == 'technical' || $mo == 'store' || $mo == 'operatingcity') {
                    $model->where('uuid', $uuid)->dec('income', $amounts)->update();
                } elseif($mo == 'balance'){
                    $model->where('id', $uid)->dec('balance', $amounts)->update();
                }

                $errno = 0;
                $message = '您的提现申请己提交';
            } else {
                $errno = 1;
                $message = '申请提现失败，请联系管理员';
            }
        } else {
            $errno = 1;
            $message = '申请提现失败，提现金额不能大于帐户余额';
        }

        return $this->json(['message' => $message, 'errno' => $errno, 'data' => $data,'sss'=>$moapply]);
    }

    //生成唯一编号
    function build_no()
    {
        return date('Ymd') . substr(implode(NULL, array_map('ord', str_split(substr(uniqid(), 7, 13), 1))), 0, 8);
    }

    function modes(){
        $res = WithdrawMode::where(['status'=>1])->select()->toArray();
        return $this->json(['data'=>$res]);
    }
}
